﻿using Share;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public static class LineRenderExtensions
{
    // 使用LineRenderer绘制矩形边框
    public static void DrawRect(this LineRenderer line, Vector2 min, Vector2 max, Color color, float width = 1.0f)
    {
        if (line == null) return;

        // 设置LineRenderer属性
        line.material = new Material(Shader.Find("Sprites/Default"));
        line.startColor = color;
        line.endColor = color;
        line.startWidth = width;
        line.endWidth = width;
        line.positionCount = 5; // 4个角 + 回到起点闭合
        line.loop = true;

        // 计算矩形的四个角
        Vector3 bottomLeft = new Vector3(min.x, min.y, 0);
        Vector3 bottomRight = new Vector3(max.x, min.y, 0);
        Vector3 topRight = new Vector3(max.x, max.y, 0);
        Vector3 topLeft = new Vector3(min.x, max.y, 0);

        // 设置LineRenderer的点位
        Vector3[] positions = new Vector3[5] { bottomLeft, bottomRight, topRight, topLeft, bottomLeft };
        line.SetPositions(positions);
    }

    // 使用LineRenderer绘制圆形
    public static void DrawCircle(this LineRenderer line, Vector2 center, float radius, Color color, float width = 1.0f, int segments = 36)
    {
        if (line == null) return;

        // 设置LineRenderer属性
        line.material = new Material(Shader.Find("Sprites/Default"));
        line.startColor = color;
        line.endColor = color;
        line.startWidth = width;
        line.endWidth = width;
        line.positionCount = segments + 1; // 分段数 + 回到起点闭合
        line.loop = true;

        // 计算圆形的点位
        Vector3[] positions = new Vector3[segments + 1];
        float angleStep = 360f / segments;

        for (int i = 0; i <= segments; i++)
        {
            float angle = i * angleStep * Mathf.Deg2Rad;
            float x = center.x + radius * Mathf.Cos(angle);
            float y = center.y + radius * Mathf.Sin(angle);
            positions[i] = new Vector3(x, y, 0);
        }

        // 设置LineRenderer的点位
        line.SetPositions(positions);
    }

}
